/**
 * Module dependencies.
 */
const router = require('express').Router();
const app = require('./appSetting');
const server = require('http').Server(app);
const MysqlClass = require('./utils/mysql');
// 定义数据
const mysql = new MysqlClass(require('./utils/mysqlConf'));
//require('./_watch/main');

const apis = {
    task: require('./api/tasks'),
    query: require('./api/query')
};

for (let i in apis) {
    router.get('/' + i,apis[i].bind(null,mysql,'get'));
    router.post('/' + i,apis[i].bind(null,mysql,'post'));
}

router.get('/test',(req,res) => {
    res.end('ok');
});
router.get('/test',(req,res) => {
    res.end('ok');
});

router.get('/api',function (req,res) {
    res.json({
        text: "这是一个 get 调用 api 的 json 内容返回"
    });
});
router.post('/api',function (req,res) {
    res.json({
        text: "这是一个 post 调用 api 的 json 内容返回"
    });
});

router.get('*',function(req,res) {
    res.setHeader('Content-Type', 'text/html');
    res.render('error');
});

app.use('/', router);

const port = 3080;
server.listen(port, function() {
    console.log(`port : ${port}`);
});
